package com.liujit.bms.app.controller;

import com.liujit.bms.app.service.BlogMemberService;
import com.liujit.bms.app.domain.model.BlogMember;
import com.liujit.bms.app.domain.params.BlogMemberParams;
import com.liujit.validator.group.AddGroup;
import com.liujit.validator.group.ModifyGroup;
import com.liujit.web.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 博客会员管理接口
 * @Author liujun
 * @Date 2021-07-13 11:14:16
 */
@RequiredArgsConstructor
@Api(tags = "博客会员管理接口")
@RestController
@RequestMapping("/blog/member")
public class BlogMemberController {

    private final BlogMemberService blogMemberService;

    @ApiOperation(value = "添加", notes = "根据博客会员添加请求参数，添加博客会员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "博客会员添加请求参数", dataType = "BlogMemberParams", dataTypeClass = BlogMemberParams.class, paramType = "body") })
    @PostMapping("add")
    public BlogMember add(@RequestBody @Validated(AddGroup.class) BlogMemberParams params) {
        return blogMemberService.save(params);
    }

    @ApiOperation(value = "修改", notes = "根据博客会员修改请求参数，修改博客会员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "博客会员修改请求参数", dataType = "BlogMemberParams", dataTypeClass = BlogMemberParams.class, paramType = "body") })
    @PostMapping("/modify")
    public BlogMember modify(@RequestBody @Validated(ModifyGroup.class) BlogMemberParams params) {
        params.setId(JwtUtil.getUserId());
        return blogMemberService.save(params);
    }

    @ApiOperation(value = "查询", notes = "根据博客会员openId，查询博客会员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "博客会员openId", dataType = "String", dataTypeClass = String.class, paramType = "query") })
    @GetMapping("/getByOpenId")
    public BlogMember getByOpenId(String openId) {
        return blogMemberService.getByOpenId(openId);
    }
}
